#include <iostream>
#include <fstream>
#include <array>
#include <cstdint>

const size_t BLOCK_SIZE = 16;  // 128 位 = 16 字节

// 读取文件中的 128 位（16 字节）并返回
bool read_block(std::ifstream& file, std::array<uint8_t, BLOCK_SIZE>& block) {
    file.read(reinterpret_cast<char*>(block.data()), BLOCK_SIZE);
    return file.gcount() == BLOCK_SIZE;  // 如果读取到 128 位，返回 true
}

// 计算 t1 和 t2 的异或
void xor_blocks(const std::array<uint8_t, BLOCK_SIZE>& t1, const std::array<uint8_t, BLOCK_SIZE>& t2, std::array<uint8_t, BLOCK_SIZE>& result) {
    for (size_t i = 0; i < BLOCK_SIZE; ++i) {
        result[i] = t1[i] ^ t2[i];
    }
}

int main(int argc, char* argv[]) {
    if (argc != 4) {
        std::cerr << "Usage: " << argv[0] << " <input_file1> <input_file2> <output_file>" << std::endl;
        return 1;
    }

    std::ifstream input_file1(argv[1], std::ios::binary);
    std::ifstream input_file2(argv[2], std::ios::binary);
    std::ofstream output_file(argv[3], std::ios::binary);

    if (!input_file1.is_open() || !input_file2.is_open() || !output_file.is_open()) {
        std::cerr << "Error opening files." << std::endl;
        return 1;
    }
    
    std::array<uint8_t, BLOCK_SIZE> t1, t2, result;
    int cnt =0;
    
    while (read_block(input_file1, t1) && read_block(input_file2, t2)) {
        xor_blocks(t1, t2, result);  // 计算异或
        output_file.write(reinterpret_cast<char*>(result.data()), BLOCK_SIZE);  // 写入结果
        cnt++;
    }

    std::cout << "Processing completed." << std::endl;

    input_file1.close();
    input_file2.close();
    output_file.close();
    std::cout<<"BLOCKS::"<<cnt<<"\n";
    return 0;
}
